package controllers.weixin;

import auth.WxMpAuth;
import models.bill.Bill;
import models.bill.BillDetail;
import models.bill.Voucher;
import models.contract.BankCard;
import models.enums.PaymentStatus;
import models.user.User;
import org.apache.commons.collections.map.HashedMap;
import play.mvc.Controller;
import play.mvc.With;
import util.common.DateUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liming on 2017/2/9.
 */

@With(WxMpAuth.class)
public class PaymentRecordController extends Controller {

    /**
     * 缴费记录列表
     */
    public static void index() {
        User user = WxMpAuth.currentUser();
        List<Map<String, Object>> billList = Bill.findPaymenListByUserId(user.id);
        List<Voucher> voucherList = Voucher.findByUserForThisMonth(user);
        BigDecimal amountVoucher = new BigDecimal("0");
        for(Voucher voucher : voucherList) {
            amountVoucher = amountVoucher.add(voucher.amount);
        }
        Map<String,Object> voucherMap = new HashMap<String,Object>();
        voucherMap.put("type","voucher");
        voucherMap.put("title", DateUtil.getCurrentMonth()+"凭证");
        voucherMap.put("payment_status", PaymentStatus.NOT_PAID);
        voucherMap.put("amount",amountVoucher);
        billList.add(0,voucherMap);
        render(billList);
    }

    /**
     * 打开明细
     *@param type
     * @param billId
     */
    public static void detail(String type,String billId) {
        String nextMonth = DateUtil.getNextMonth();
        if("voucher".equals(type)) {
            User user = WxMpAuth.currentUser();
            List<Voucher> voucherList = Voucher.findByUserForThisMonth(user);
            String bankCardNo = "";
            Bill bill = new Bill();
            bill.paymentStatus = PaymentStatus.NOT_PAID;
            List<BillDetail> billDetails = new ArrayList<BillDetail>();
            if (voucherList != null && voucherList.size() > 0) {
                BigDecimal amountVoucher = new BigDecimal("0");
                BankCard bankCard = BankCard.findByContract(voucherList.get(0).contract);
                if(bankCard != null && bankCard.bankCardNumber!=null && bankCard.bankCardNumber.length()>4){
                    bankCardNo = bankCard.bankCardNumber.substring(bankCard.bankCardNumber.length()-4);
                }
//                bill.code = voucherList.get(0).contract.code;
                bill.paymentStatus = PaymentStatus.NOT_PAID;
                for(Voucher voucher : voucherList) {
                    BillDetail billDetail = new BillDetail();
                    billDetail.feeType = voucher.feeType;
                    billDetail.amount = voucher.amount;
                    billDetails.add(billDetail);
                    amountVoucher = amountVoucher.add(voucher.amount);
                }
                bill.amount = amountVoucher;
            }
            render(bill, billDetails, bankCardNo,nextMonth);
        }else {

            Bill bill = Bill.findByBillId(billId);
            System.out.println("sda"+bill.paymentStatus);
            List<BillDetail> billDetails = BillDetail.loadByBillId(billId);
            BankCard bankCard = BankCard.findByContract(bill.contract);
            String bankCardNo = "";
            if(bankCard != null && bankCard.bankCardNumber!=null && bankCard.bankCardNumber.length()>4){
                bankCardNo = bankCard.bankCardNumber.substring(bankCard.bankCardNumber.length()-4);
            }
            render(bill, billDetails, bankCardNo,nextMonth);
        }
    }
}
